package com.ha8.netease.views.components
{
	import com.ha8.netease.views.events.PageEvent;
	
	import flash.events.MouseEvent;
	
	import spark.components.Button;
	import spark.components.supportClasses.SkinnableComponent;
	
	[Event(name="change", type="com.ha8.netease.views.events.PageEvent")]
	
	/**
	 * 分页按钮
	 * 
	 * @author rogo
	 */	
	public class Ha8Page extends SkinnableComponent
	{
		public function Ha8Page()
		{
			super();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Skin Part
		//
		//--------------------------------------------------------------------------
		
		[SkinPart]
		public var preBtn:Button;
		
		[SkinPart]
		public var nextBtn:Button;
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var _currentPage:int = 1;

		public function get currentPage():int
		{
			return _currentPage;
		}

		public function set currentPage(value:int):void
		{
			if (value < 1)
				return;
			
			_currentPage = value;
			
			updateDisplay();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			if (instance == preBtn)
			{
				preBtn.addEventListener(MouseEvent.CLICK, preBtn_clickHandler);
				if (currentPage == 1)
					preBtn.enabled = false;
			}else if (instance == nextBtn)
			{
				nextBtn.addEventListener(MouseEvent.CLICK, nextBtn_clickHandler);
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function updateDisplay():void
		{
			if (currentPage == 1 && preBtn)
				preBtn.enabled = false;
			else
				preBtn.enabled = true;
		}
		
		//--------------------------------------------------------------------------
		//
		//	Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function preBtn_clickHandler(e:MouseEvent):void
		{
			var pageEvent:PageEvent = new PageEvent(PageEvent.CHANGE);
			pageEvent.oldPage = currentPage--;
			pageEvent.newPage = currentPage;
			dispatchEvent(pageEvent);
		}
		
		private function nextBtn_clickHandler(e:MouseEvent):void
		{
			var pageEvent:PageEvent = new PageEvent(PageEvent.CHANGE);
			pageEvent.oldPage = currentPage++;
			pageEvent.newPage = currentPage;
			dispatchEvent(pageEvent);
		}
		
	}
}